package file.network;

import java.io.IOException;
import java.net.Socket;

import file.BadFileTypeException;
import file.TransferFile;
import file.TransferFileObserver;

public class FileReceiver extends Thread {

	Socket sender ;
	TransferFile file ;
	TransferFileObserver[] tfos ;
	
	/**
	 * Create a new FileReceiver that will receive the file specified from
	 * the socket sender and notify all the observers in tfos.
	 * 
	 * @param file The file to receive
	 * @param tfos The observer to notify on transfer update
	 * @param sender A socket accepted from the sender of the file
	 */
	public FileReceiver (TransferFile file, TransferFileObserver[] tfos, Socket sender) {
		this.file = file ;
		this.tfos = tfos ;
		this.sender = sender ;
	}
	
	public void run () {
		try {
			this.file.receive(this.sender.getInputStream(), this.tfos) ;
			this.sender.close() ;
		} 
		catch (IOException e) {
			e.printStackTrace();
		} 
		catch (BadFileTypeException e) {
			e.printStackTrace();
		}
	}
	
}
